<?php
/**
 * 匠言知识付费
 * Author: 山西匠言网络科技有限公司
 * This is not a free software, it under the license terms, you can visit https://www.zsffzxkc.cn/ get more details.
 */
namespace app\index\controller;
use think\Db;

/**
 * Class 匠言知识付费核心知识课程业务
 * @package app\index\controller
 */
class Courses extends  Base
{
//    PC端分类课程接口
    public function index_pc_courselist(){
        $post=input('post.');
        $map=array(
            'status'=>1,
            'acid'=>1
        );

        $db=db('coursemenu');
        if(isset($post['flid'])){
            $coursemenu=$db->where($map)->where('fenleiid','like','%'.$post['flid'].'%')->select();
        }else{
            $coursemenu=$db->where($map)->select();
        }
        if(!empty($coursemenu)){
            foreach ($coursemenu as $k=>$v){
                $coursemenu[$k]['thumb']=to_media_url($v['thumb']);
                $tinfo=db('teacher')->where(['acid'=>1,'is_verify'=>1,'id'=>$v['tid']])->find();
                $ksnum=db('course')->where(['acid'=>1,'status'=>1,'menuid'=>$v['id']])->count();
                $coursemenu[$k]['tinfo']=$tinfo;
                $coursemenu[$k]['ksnum']=$ksnum;
                if($v['media']=='audio'){
                    $coursemenu[$k]['mediatext']='音频';
                }elseif($v['media']=='video'){
                    $coursemenu[$k]['mediatext']='视频';
                }elseif($v['media']=='tuwen'){
                    $coursemenu[$k]['mediatext']='图文';
                }elseif($v['media']=='all'){
                    $coursemenu[$k]['mediatext']='混合';
                }
                if($v['lid']!=0){
                    $levelinfo = db('viplevel')->where(['acid'=>1,'id'=>$v['lid']])->find();
                    $coursemenu[$k]['levelname']=$levelinfo['name'];
                }else{
                    $coursemenu[$k]['levelname']='不限';
                }
            }
            $this->result(0,'获取课程列表数据成功',$coursemenu,0,'json');
        }
    }
    //全部课程接口
    public function allcourses(){
        $courses=db('coursemenu')->where(['acid'=>1,'status'=>1,'is_verify'=>1])->select();
        foreach ($courses as $k=>$v){
            $ksnum=db('course')->where(['acid'=>1,'status'=>1,'menuid'=>$v['id']])->count();
            $courses[$k]['ksnum']=$ksnum;
            $courses[$k]['thumb']=to_media_url($v['thumb']);
            if($v['media']=='audio'){
                $courses[$k]['mediatext']='音频';
            }elseif($v['media']=='video'){
                $courses[$k]['mediatext']='视频';
            }elseif($v['media']=='tuwen'){
                $courses[$k]['mediatext']='图文';
            }
            if($v['lid']!=0){
                $levelinfo = db('viplevel')->where(['acid'=>1,'id'=>$v['lid']])->find();
                $courses[$k]['levelname']=$levelinfo['name'];
            }else{
                $courses[$k]['levelname']='不限';
            }
        }
        if($courses){
            $this->result(0,'获取所有课程数据成功',$courses,0,'json');
        }else{
            $this->result(1,'暂无课程','',0,'json');
        }
    }
    // 打卡
    public function daka(){
        $post=input('post.');
        $post['addtime']=time();
        $post['acid']=1;
        $res = db('daka')->insert($post);
        if($res){
            $this->result(0,'打卡成功','',0,'json');
        }else{
            $this->result(1,'失败','',0,'json');
        }
    }
    //构造课程目录下拉选项
    public function menu_select_json(){
        $map2=array(
            'status'=>1,
            'acid'=>1
        );
        $classify=Db::name('coursemenu')
            ->where($map2)
            ->field('id,menuname')
            ->select();
        foreach ($classify as $k=>$v){
            $list[$k]['value']=$v['id'];
            $list[$k]['name']=$v['menuname'];
        }
        $this->result(0,'获取课程目录成功',$list,0,'json');
    }
    //构造课程分类下拉选项
    public function fl_select_json(){
        $map2=array(
            'status'=>1,
            'acid'=>1,
            'pid'=>0
        );
        $classify=Db::name('classify')
            ->where($map2)
            ->select();
        foreach ($classify as $k=>$v){
            $list[$k]['id']=$v['id'];
            $list[$k]['name']=$v['title'];
            $child=Db::name('classify')
                ->where([
                    'status'=>1,
                    'acid'=>1,
                    'pid'=>$v['id']
                ])
                ->field('id,title as name')
                ->select();
            $list[$k]['children'] = $child;
        }
        $this->result(0,'获取课程分类成功',$list,0,'json');
    }
    public function add_course(){
        $post=input('post.');
        if(isset($post['tid']) && $post['tid']!=''){
            $tinfo=db('teacher')->where(['acid'=>1,'id'=>$post['tid'],'is_verify'=>1])->find();
            if(!empty($tinfo)){
                $data=[
                    'addtime'=>time(),
                    'acid'=>1,
                    'status'=>1,
                    'xh'=>0
                ];
                unset($post['tid']);
                $data1=array_merge($post,$data);
                $res=db('course')->insert($data1);
                if($res){
                    $this->result(0,'添加课程成功','',0,'json');
                }else{
                    $this->result(1,'添加课程失败','',0,'json');
                }
            }else{
                $this->result(1,'讲师未审核或为测试账号','',0,'json');
            }
        }else{
            $this->result(1,'非法操作！','',0,'json');
        }
    }
    public function add_menu(){
        $post=input('post.');
        if(isset($post['tid']) && $post['tid']!=''){
            $tinfo=db('teacher')->where(['acid'=>1,'id'=>$post['tid'],'is_verify'=>1])->find();
            if(!empty($tinfo)){
                $data=[
                    'addtime'=>time(),
                    'acid'=>1,
                    'status'=>1,
                    'is_fx'=>0,
                    'lid'=>0
                ];
                $data1=array_merge($post,$data);
                $res=db('coursemenu')->insert($data1);
                if($res){
                    $this->result(0,'添加课程目录成功','',0,'json');
                }else{
                    $this->result(1,'添加课程目录失败','',0,'json');
                }
            }else{
                $this->result(1,'讲师未审核或为测试账号','',0,'json');
            }
        }else{
            $this->result(1,'非法操作！','',0,'json');
        }
    }
    public function autowrap($fontsize, $angle, $fontface, $string, $width) {
        // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
        $content = "";

        // 将字符串拆分成一个个单字 保存到数组 letter 中
        for ($i=0;$i<mb_strlen($string);$i++) {
            $letter[] = mb_substr($string, $i, 1);
        }

        foreach ($letter as $l) {
            $teststr = $content." ".$l;
            $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
            // 判断拼接后的字符串是否超过预设的宽度
            if (($testbox[2] > $width) && ($content !== "")) {
                $content .= "\n";
            }
            $content .= $l;
        }
        return $content;
    }
    public function getgoodsFinal($nickname,$goodsimg,$goodsname,$avatar,$hbimg,$imgkey,$qrimg,$userid){
        $acid=1;
        $map=array(
            'acid'=>1,
        );
        $cert=Db::name('set_cert')
            ->where($map)
            ->find();
        //背景图
        $is_very = $hbimg;
        if(strlen($is_very) < 1)
        {
            return false;
        }
        $QR = imagecreatefromstring($is_very);
        //微信头像
        $wx_logo = imagecreatefromstring($avatar);
        $QR_width    = imagesx($QR);//背景图宽度
        $QR_height   = imagesy($QR);//背景图高度

        $wx_logo_width  = imagesx($wx_logo);
        $wx_logo_height = imagesy($wx_logo);
        //载入文字
        //字体
        $font_file = ROOT_PATH .'public/font/bdzy.ttf';
        //字体颜色
        $color = imagecolorallocate($QR, 255, 154, 40);
        $black = imagecolorallocate($QR, 0, 0, 0);
        if($imgkey==0){
            //载入头像
            imagecopyresampled($QR, $wx_logo, $QR_width*0.1, $QR_height*0.1, 0, 0, 100, 100, $wx_logo_width, $wx_logo_height);
            // 昵称
            imagefttext($QR, 20, 0, $QR_width*0.1+145, $QR_height*0.1+25, $black, $font_file,$nickname);
            // 商品标题
            $goodstitle = $this->autowrap(35, 0, $font_file, "“ ".$goodsname." ”", $QR_width-80);
            $blue = imagecolorallocate($QR,46,92,241);
            imagefttext($QR, 35, 0, $QR_width*0.1, $QR_height*0.53, $blue, $font_file, $goodstitle);
            //载入商品图片
            $goods_img_string=$goodsimg;
            $goods_img=imagecreatefromstring($goods_img_string);
            $goods_img_width  = imagesx($goods_img);
            $goods_img_height = imagesy($goods_img);
            imagecopyresampled($QR, $goods_img, $QR_width*0.5-300, $QR_height*0.2, 0, 0, 600, 340, $goods_img_width, $goods_img_height);
            //载入二维码
            $qr_img_string=$qrimg;
            $qr_img=imagecreatefromstring($qr_img_string);
            $qr_img_width  = imagesx($qr_img);
            $qr_img_height = imagesy($qr_img);
            imagecopyresampled($QR, $qr_img, $QR_width*0.5-90, $QR_height*0.67, 0, 0, 180, 180, $qr_img_width, $qr_img_height);
        }elseif($imgkey==1){
            // 证书名称
            imagefttext($QR, 50, 0, $QR_width*0.4, $QR_height*0.28, $black, $font_file,$cert['school']);
            //载入头像
            imagecopyresampled($QR, $wx_logo, $QR_width*0.1, $QR_height*0.34, 0, 0, 100, 100, $wx_logo_width, $wx_logo_height);
            // 昵称
            imagefttext($QR, 31, 0, $QR_width*0.1+135, $QR_height*0.39, $black, $font_file,$nickname);
            // 商品标题
            $goodstitle = $this->autowrap(30, 0, $font_file, $goodsname, $QR_width-100);
            $blue = imagecolorallocate($QR,46,92,241);
            imagefttext($QR, 30, 0, $QR_width*0.3-45, $QR_height*0.48+10, $blue, $font_file, $goodstitle);
            // 结业证书
            imagefttext($QR, 30, 0, $QR_width*0.5, $QR_height*0.55-15, $black, $font_file,$cert['certname']);
            // 证书编号、颁发时间
            imagefttext($QR, 28, 0, $QR_width*0.1+135, $QR_height*0.78-20, $black, $font_file,$cert['certnum_pre'].$userid);
            imagefttext($QR, 28, 0, $QR_width*0.1+135, $QR_height*0.82-5, $black, $font_file,date('Y年m月d日',time()));
            //载入商品图片
            $goods_img_string=$goodsimg;
            $goods_img=imagecreatefromstring($goods_img_string);
            $goods_img_width  = imagesx($goods_img);
            $goods_img_height = imagesy($goods_img);
            imagecopyresampled($QR, $goods_img, $QR_width*0.63, $QR_height*0.67, 0, 0, 600, 300, $goods_img_width, $goods_img_height);
            //载入二维码
            $qr_img_string=$qrimg;
            $qr_img=imagecreatefromstring($qr_img_string);
            $qr_img_width  = imagesx($qr_img);
            $qr_img_height = imagesy($qr_img);
            imagecopyresampled($QR, $qr_img, $QR_width*0.83, $QR_height*0.12, 0, 0, 150, 150, $qr_img_width, $qr_img_height);
        }elseif($imgkey==2){
            //载入头像
            imagecopyresampled($QR, $wx_logo, $QR_width*0.13, $QR_height*0.26, 0, 0, 80, 80, $wx_logo_width, $wx_logo_height);
            // 昵称
            imagefttext($QR, 20, 0, $QR_width*0.13+112, $QR_height*0.26+25, $black, $font_file,$nickname."的结业证书");
            // 商品标题
            $goodstitle = $this->autowrap(30, 0, $font_file, "“ ".$goodsname." ”", $QR_width-100);
            $blue = imagecolorallocate($QR,46,92,241);
            imagefttext($QR, 30, 0, $QR_width*0.1, $QR_height*0.35+370, $blue, $font_file, $goodstitle);
            //载入商品图片
            $goods_img_string=$goodsimg;
            $goods_img=imagecreatefromstring($goods_img_string);
            $goods_img_width  = imagesx($goods_img);
            $goods_img_height = imagesy($goods_img);
            imagecopyresampled($QR, $goods_img, $QR_width*0.5-275, $QR_height*0.35, 0, 0, 550, 315, $goods_img_width, $goods_img_height);
            //载入二维码
            $qr_img_string=$qrimg;
            $qr_img=imagecreatefromstring($qr_img_string);
            $qr_img_width  = imagesx($qr_img);
            $qr_img_height = imagesy($qr_img);
            imagecopyresampled($QR, $qr_img, $QR_width*0.63, $QR_height*0.75, 0, 0, 150, 150, $qr_img_width, $qr_img_height);
        }
        //每次发送指令每次重新生成二维码
        $filedir=ROOT_PATH . 'public/goodshb/'.$acid.'/'.$userid.'/';
        is_dir($filedir) OR mkdir($filedir,0777,true);
        $file_path = $filedir.time()."-".$imgkey.'.jpg';
        $res2=imagejpeg($QR,$file_path);
        if($res2){
            $hbpath='/goodshb/'.$acid.'/'.$userid."/".time()."-".$imgkey.'.jpg';
            return $hbpath;
        }
        //销毁资源
        imagedestroy($QR);
    }
    public function hbhc(){
        $post=input('post.');
        $map=array(
            'acid'=>1,
        );
        $res=Db::name('set_cert')
            ->where($map)
            ->find();
        if(!empty($post['imgkey'])){
            $imgsrc=to_media_url($res['img']);
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $imgsrc);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $output1 = curl_exec($curl);
            curl_close($curl);
            $hbimg=$output1;
        }

        $userinfo=Db::name('user')
            ->where($map)
            ->where('id',$post['userid'])
            ->find();
        if($userinfo['nickname']!='' && $userinfo['avatar']!=''){
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $userinfo['avatar']);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($curl);
            curl_close($curl);
            $avatar_str=$output;

            // $Qr_img=$this->getQr();//生成为公众号带参数临时二维码
            $h5_set=Db::name('set_h5')
                ->where($map)
                ->find();
            $url=$h5_set['url'].'?userid='.$userinfo['id'];
            $path = false;
            $level = 'L';
            $size = '8';
            $margin ='2';
            if($post['apptype']=='H5' || $post['apptype']=='APP-PLUS'){
                vendor("phpqrcode.phpqrcode");
                $qrcode = new \QRcode();
                ob_start();
                $qrcode->png($url, $path, $level, $size, $margin);
                $Qr_img =ob_get_contents();
                ob_end_clean();
            }elseif($post['apptype']=='MP' || $post['apptype']=='MP-WEIXIN'){
                $access_token = $this->get_access_token('MP-WEIXIN');
                $access_token=json_decode($access_token,true);
                $url="https://api.weixin.qq.com/wxa/getwxacode?access_token=".$access_token['access_token'];
                $data1 = [
                    'path'=>"pages/index/index?pid=".$userinfo['id'],
                    'is_hyaline'=>false
                ];
                $data=json_encode($data1);
                $Qr_img =$this->http_curl($url,'post',$data);
            }
            $goodsinfo = db('coursemenu')->where($map)->where(['id'=>$post['menuid']])->find();
            if(!empty($goodsinfo)){
                $curl = curl_init();
                curl_setopt($curl, CURLOPT_URL, to_media_url($goodsinfo['thumb']));
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                $output = curl_exec($curl);
                curl_close($curl);
                $goodsimg_str=$output;
            }
            // 生成首页带参数链接二维码
            if ($avatar_str && $Qr_img) {
                $hbpath=$this->getgoodsFinal($userinfo['nickname'],$goodsimg_str,$goodsinfo['menuname'],$avatar_str,$hbimg,1,$Qr_img,$post['userid']);
                if(!empty($hbpath)){
                    $hbpath = to_media_url($hbpath);
                    $this->result(0,'合成海报成功',['imgpath'=>$hbpath],0,'json');
                }else{
                    $this->result(1,'合成失败','',0,'json');
                }
            }
        }else{
            $this->result(1,'合成失败','',0,'json');
        }
    }
    public function search_course(){
        $post=input('post.');
        $map=array(
            'status'=>1,
            'acid'=>1
        );
        $db=db('coursemenu');
        switch ($post['keyword']){
            case 'hot':
                $coursemenu=$db->where($map)->order('viewnum DESC')->limit(50)->select();
                break;
            case 'free':
                $coursemenu=$db->where($map)->where('price',0)->select();
                break;
            case 'new':
                $coursemenu=$db->where($map)->order('addtime DESC')->limit(50)->select();
                break;
            default:
                $coursemenu=$db->where($map)->where('menuname','like','%'.$post['keyword'].'%')->select();
                break;
        }
        if(!empty($coursemenu)){
            foreach ($coursemenu as $k=>$v){
                $coursemenu[$k]['thumb']=to_media_url($v['thumb']);
                $tinfo=db('teacher')->where(['acid'=>1,'is_verify'=>1,'id'=>$v['tid']])->find();
                $ksnum=db('course')->where(['acid'=>1,'status'=>1,'menuid'=>$v['id']])->count();
                $coursemenu[$k]['tinfo']=$tinfo;
                $coursemenu[$k]['ksnum']=$ksnum;
                if($v['lid']!=0){
                    $levelinfo = db('viplevel')->where(['acid'=>1,'id'=>$v['lid']])->find();
                    $coursemenu[$k]['levelname']=$levelinfo['name'];
                }else{
                    $coursemenu[$k]['levelname']='不限';
                }
            }
            $this->result(0,'搜索课程列表数据成功',$coursemenu,0,'json');
        }
    }
    public function indexgoods(){
        $post=input('post.');
        $map=array(
            'status'=>1,
            'acid'=>1
        );
        $db=db('coursemenu');
        if($post['goods_label']=='is_tj'){
            $coursemenu=$db->where($map)->where('fenleiid','like','%'.$post['flid'].'%')->where('is_tj',1)->limit(4)->select();
        }elseif ($post['goods_label']==''){
            $coursemenu=$db->where($map)->where('fenleiid','like','%'.$post['flid'].'%')->limit(6)->select();
        }
        if(!empty($coursemenu)){
            foreach ($coursemenu as $k=>$v){
                $coursemenu[$k]['thumb']=to_media_url($v['thumb']);
                $tinfo=db('teacher')->where(['acid'=>1,'is_verify'=>1,'id'=>$v['tid']])->find();
                $ksnum=db('course')->where(['acid'=>1,'status'=>1,'menuid'=>$v['id']])->count();
                $coursemenu[$k]['tinfo']=$tinfo;
                $coursemenu[$k]['ksnum']=$ksnum;
                if($v['lid']!=0){
                    $levelinfo = db('viplevel')->where(['acid'=>1,'id'=>$v['lid']])->find();
                    $coursemenu[$k]['levelname']=$levelinfo['name'];
                }else{
                    $coursemenu[$k]['levelname']='不限';
                }
            }
            $this->result(0,'获取一级目录课程列表数据成功',$coursemenu,0,'json');
        }
    }
    public function comment(){
        $data=input('post.');
        $data['addtime']=time();
        $data['is_verify']=0;
        $data['acid']=1;
        $res=db('comment')->insert($data);
        if($res){
            $this->result(0,'评价成功,等待审核','',0,'json');
        }
    }
    public function dingyue(){
        $post=input('post.');
        $map=array(
            'acid'=>1,
            'menuid'=>$post['menuid'],
            'goodstype'=>$post['goodstype'],
            'uid'=>$post['uid']
        );
        $dyinfo=db('dingyue')->where($map)->find();
        if(empty($dyinfo)){
            $post['is_dingyue']=1;
            $post['acid']=1;
            $res = db('dingyue')->insert($post);
            if($res){
                $this->result(0,'订阅成功',$post['is_dingyue'],0,'json');
            }
        }else{
            $is_dingyue=$dyinfo['is_dingyue']==1?0:1;
            $res=db('dingyue')->where($map)->update(['is_dingyue'=>$is_dingyue]);
            if($res){
                $this->result(0,'修改订阅成功',$is_dingyue,0,'json');
            }
        }
    }
    public function index(){
        $menuid=input('post.menuid');
        $uid=input('post.uid');
        $map=array(
            'acid'=>1,
            'menuid'=>$menuid
        );
        $menuinfo=Db::name('coursemenu')
            ->where(['acid'=>1,'id'=>$menuid])
            ->find();
        if($menuinfo['is_yxq']==1){
            if(isset($menuinfo['yxqdata'])){
                $yxqdata = json_decode($menuinfo['yxqdata'],true);
                $new_yxqdata = array();
                foreach ($yxqdata['column'] as $k=>$v){
                    $yxqinfo = db('course_yxq')->where(['acid'=>1,'id'=>$v['yxqid']])->find();
                    $v['name'] = $yxqinfo['name'];
                    array_push($new_yxqdata,$v);
                }
                $menuinfo['new_yxqdata'] = $new_yxqdata;
            }
        }
        if(isset($uid) && $uid != ''){
            $menu_pay=db('mediaorder')->where($map)->where(['is_pay'=>1,'goodstype'=>'course','sonid'=>0,'uid'=>$uid])->order('id DESC')->limit(1)->select();
            if(!empty($menu_pay)){
                $menu_pay=$menu_pay[0];
                if($menu_pay['yxqid']!=0 && $menu_pay['endtime'] != 0){
                    if($menu_pay['endtime'] >= time()) {
                        $menuinfo['is_pay']=1;
                        if($menu_pay['endtime'] > 0) {
                            $menuinfo['endtime_cn']=date('Y-m-d H:i',$menu_pay['endtime']);
                        }
                    }else{
                        $menuinfo['is_pay']=0;
                    }
                }else{
                    $menuinfo['is_pay']=1;
                }
            }else{
                $menuinfo['is_pay']=0;
            }
        }else{
            $menuinfo['is_pay']=0;
        }
        Db::name('coursemenu')
            ->where(['acid'=>1,'id'=>$menuid])
            ->setInc('viewnum');
        $courses=Db::name('course')
            ->where($map)
            ->where('status',1)
            ->order('xh ASC')
            ->select();
        foreach ($courses as $k=>$v){
            if(isset($uid) && $uid != '') {
                $progress = db('study_percent')->where($map)->where(['sonid' => $v['id'], 'uid' => $uid])->find();
                if (!empty($progress)) {
                    $courses[$k]['progress'] = $progress['progress'];
                } else {
                    $courses[$k]['progress'] = 0;
                }
                $son_pay = db('mediaorder')->where($map)->where(['is_pay' => 1, 'goodstype' => 'course', 'sonid' => $v['id'], 'uid' => $uid])->find();
                if (!empty($son_pay)) {
                    $courses[$k]['is_pay'] = 1;
                } else {
                    $courses[$k]['is_pay'] = 0;
                }
            }else{
                $courses[$k]['is_pay'] = 0;
                $courses[$k]['progress'] = 0;
            }
            $set=Db::name('storage')->where('acid',1)->find();
            $mode = $set['upload_area'];
            $courses[$k]['src'] = to_files_path($courses[$k]['src'],$mode);
        }
        if(!empty($menuinfo)){
            $teacher=Db::name('teacher')
                ->where(['acid'=>1,'id'=>$menuinfo['tid']])
                ->find();
            unset($teacher['tpassword']);
            $teacher['img'] = to_media_url($teacher['img']);

            $files = Db::name('ziyuan')
                ->where(['acid'=>1,'menuid'=>$menuinfo['id'],'media'=>'course'])
                ->select();
            foreach ($files as $k=>$v) {
                if ($v['type'] == '1') {
                    $media = '图片';
                } elseif ($v['type'] == '2') {
                    $media = '表格';
                } elseif ($v['type'] == '3') {
                    $media = 'PDF';
                }elseif ($v['type'] == '4') {
                    $media = 'Word';
                }elseif ($v['type'] == '5') {
                    $media = 'PPT';
                }elseif ($v['type'] == '0') {
                    $media = '未知';
                }
                $files[$k]['media']=$media;
                if ($v['upload_area'] == '0') {
                    $files[$k]['upload_area']='本地';
                }else if ($v['upload_area'] == '1') {
                    $files[$k]['upload_area']='七牛';
                }else if ($v['upload_area'] == '2') {
                    $files[$k]['upload_area']='阿里';
                }else if ($v['upload_area'] == '3') {
                    $files[$k]['upload_area']='腾讯';
                }
                $files[$k]['addtime']=date('Y-m-d H:i:s',$v['addtime']);
                $files[$k]['path']=to_files_path($v['path'],$v['upload_area']);
            }
            //作业打卡列表
            $dianpings = Db::name('daka')
                ->where(['acid'=>1,'menuid'=>$menuinfo['id'],'is_gk'=>1])
                ->select();
            foreach ($dianpings as $k=>$v){
                $user=db('user')->where(['acid'=>1,'id'=>$v['uid']])->find();
                $dianpings[$k]['nickname']=$user['nickname'];
                $dianpings[$k]['uid']=$user['id'];
                $dianpings[$k]['avatar']= to_media_url($user['avatar']);
                $dianpings[$k]['addtime']=date('Y-m-d H:i:s',$v['addtime']);
                if($v['dptime'] && $v['dptime']!='' && $v['dptime']!='null'){
                    $dianpings[$k]['dptime'] = date('Y-m-d H:i:s',$v['dptime']);
                }
            }
            if($menuinfo['lid']!=0) {
                $levelinfo = db('viplevel')->where(['acid' => 1, 'id' => $menuinfo['lid']])->find();
                $menuinfo['levelname'] = $levelinfo['name'];
            }else{
                $menuinfo['levelname'] = '不限';
            }
            $menuinfo['thumb'] = to_media_url($menuinfo['thumb']);
            $comment=db('comment')->where($map)->where('is_verify',1)->order('addtime desc')->select();
            foreach ($comment as $k=>$v){
                $user=db('user')->where(['acid'=>1,'id'=>$v['uid']])->find();
                $comment[$k]['nickname']=$user['nickname'];
                $comment[$k]['avatar']= to_media_url($user['avatar']);
                $comment[$k]['addtime']=date('Y-m-d H:i:s',$v['addtime']);
            }
        }
        $data=array(
            'menuinfo'=>$menuinfo,
            'courses'=>$courses,
            'teacher'=>$teacher,
            'comment'=>$comment,
            'files'=>$files,
            'dianpings'=>$dianpings
        );
        if(isset($uid) && $uid != '') {
            $dingyue = db('dingyue')->where(['acid' => 1, 'menuid' => $menuid, 'goodstype' => 'course', 'uid' => input('post.uid')])->find();
            if (!empty($dingyue)) {
                $data['is_dingyue'] = $dingyue['is_dingyue'];
            } else {
                $data['is_dingyue'] = 0;
            }
        }else{
            $data['is_dingyue'] = 0;
        }
        $this->result(0,'获取单目录课程列表数据成功',$data,0,'json');
    }
//    课程目录或子课程详情
    public function courseinfo(){
        $menuid=input('post.menuid');
        $sonid=input('post.sonid');
        $map=array(
            'acid'=>1,
            'id'=>$menuid
        );
        $map2=array(
            'acid'=>1,
            'menuid'=>$menuid,
            'id'=>$sonid,
            'status'=>1
        );
        if($sonid==''){
            Db::name('coursemenu')
                ->where($map)
                ->setInc('viewnum');
            $info=Db::name('coursemenu')
                ->where($map)
                ->find();
        }else{
            $info1=Db::name('coursemenu')
                ->where($map)
                ->find();
            $info=Db::name('course')
                ->where($map2)
                ->find();
            $info['thumb']=$info1['thumb'];
        }
        $info['thumb'] = to_media_url($info['thumb']);
        if(!empty($info)){
            $this->result(0,'获取课程信息数据成功',$info,0,'json');
        }
    }
    public function tuwendetail(){
        $twid=input('post.twid');
        $map=array(
            'acid'=>1,
            'id'=>$twid
        );
        $twinfo=Db::name('course')
            ->where($map)
            ->find();
        if(!empty($twinfo)){
            $this->result(0,'获取图文详情数据成功',$twinfo,0,'json');
        }
    }
}
